package com.risetechnology.krystalkitchen1;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.risetechnology.adapter.IngredientListAdapter;
import com.risetechnology.common.Const;
import com.risetechnology.common.Shared;
import com.risetechnology.data.Ingredient;
import com.risetechnology.master.NavigationActivity;
import com.risetechnology.master.NavigationGroup;

public class ActivityAddIngredients extends NavigationActivity {

	public static ArrayList<Ingredient> ingredients;
	
	private TextView textviewSelectAll;
	private TextView textviewDeselectAll;
	private TextView textviewAdd;

	private ListView listviewIngredients;
	private List<Ingredient> ingredientList;
	private IngredientListAdapter listAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_add_ingredients);

		setContents();
	}

	private void setContents() {
		textviewSelectAll = (TextView) findViewById(R.id.tv_select_all);
		textviewSelectAll.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				selectAll(true);
			}
		});

		textviewDeselectAll = (TextView) findViewById(R.id.tv_deselect_all);
		textviewDeselectAll.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				selectAll(false);
			}
		});

		textviewAdd = (TextView) findViewById(R.id.tv_add);
		textviewAdd.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				//TabsManager.goToTab(3);
				Intent intent = new Intent(ActivityAddIngredients.this.getParent(), ActivitySelectShoppingList.class);
				ingredients = new ArrayList<Ingredient>();
				
				for (int i = 0; i < ingredientList.size(); i++) {
					if (ingredientList.get(i).isSelected()) {
						ingredients.add(ingredientList.get(i));
					}
				}
				if (ingredients.size()>0) {
					
					NavigationGroup parent = ((NavigationGroup) getParent());
					if (parent != null) {
						parent.backWithoutChange();
					}
					
					
					intent.putExtra(Const.KEY_INGREDIENTS, ingredients);
					goNextHistory("Select Shopping List", intent);
				} else {
					Toast.makeText(ActivityAddIngredients.this.getParent(), "Please select ingredient(s) to add!", Toast.LENGTH_LONG).show();
				}
			}
		});

		listviewIngredients = (ListView) findViewById(R.id.lv_ingredients);
		ingredientList = new ArrayList<Ingredient>();
		listAdapter = new IngredientListAdapter(ActivityAddIngredients.this.getParent(), R.layout.list_item_ingredient, ingredientList);
		listviewIngredients.setAdapter(listAdapter);

		getIngredients();
	}

	private void getIngredients() {
		if (ingredientList == null) {
			ingredientList = new ArrayList<Ingredient>();
		} else {
			ingredientList.clear();
		}

		int recipeId = getIntent().getIntExtra(Const.KEY_ID, 0);
		if (recipeId > 0) {
			ingredientList.addAll(Shared.dbAdapter.getIngByRecipe(recipeId, Ingredient.INGRE1));
			ingredientList.addAll(Shared.dbAdapter.getIngByRecipe(recipeId, Ingredient.INGRE2));
			ingredientList.addAll(Shared.dbAdapter.getIngByRecipe(recipeId, Ingredient.INGRE3));
		}
		
		listAdapter.notifyDataSetChanged();
	}
	
	private void selectAll(boolean isSelected) {
		int length = ingredientList.size();
		
		for (int i = 0; i < length; i++) {
			ingredientList.get(i).setSelected(isSelected);
		}
		
		listAdapter.notifyDataSetChanged();
	}
}
